package edu.chl.da.ejb.ctrl;

import edu.chl.da.ejb.core.Airplane;
import edu.chl.da.ejb.core.Seat;
import edu.chl.da.ejb.core.SeatPK;
import edu.chl.da.ejb.persistency.IAirplaneFacade;
import edu.chl.da.ejb.persistency.ISeatFacade;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Max
 */
@Stateless
public class ManageSeat implements IManageSeat {

    @EJB
    IAirplaneFacade iaf;

    @EJB
    ISeatFacade isf;

    @EJB
    IManageAirplane ima;

    @Override
    public boolean removeSeat(Seat seat) {
        try{
            isf.remove(seat);

        }
        catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<Seat> getSeatList(Airplane airplane) {
        List<Seat> seatList = new ArrayList<Seat>();
        seatList = isf.findAll();
        List<Seat> tempList = new ArrayList<Seat>();
        if(seatList!=null){
            for(Seat s : seatList){
                if(s.getAirplane1().getId().equals(airplane.getId())){
                    tempList.add(s);
                }
            }
        }
        return tempList;
    }

    @Override
    public Seat createSeat(Airplane airplane, int row, int no) {

        SeatPK seatPK = new SeatPK();
        seatPK.setAirplane(airplane.getId());
        seatPK.setRow(row);
        seatPK.setSeatno(no);

        Seat seat = new Seat();
        seat.setAirplane1(airplane);
        seat.setSeatPK(seatPK);

        isf.create(seat);
        return seat;
    }

    @Override
    public Seat editSeat(Seat seat) {
        isf.edit(seat);
        return seat;
    }

    @Override
    public List<Seat> getAllSeats() {
        return isf.findAll();
    }
}
